<template>
  <div class="content pt20">
    <template v-if="attributes.attribute?.length > 0">
      <div class="shuxing pb0">
        <div class="title">商品属性</div>
        <!-- 商品属性 -->
        <div class="shop-info">
          <div class="info-list">
            <div class="info-box" v-for="(item, i) in attributes.attribute" :key="i">
              <span class="info-f jzt-shs-medium">{{ item.stastsName }}:</span>
              <div class="info-r" v-for="(info, a) in item.children" style="display: flex" :key="a">
                <!-- 二级 -->
                <template v-if="item?.isViewProperty === 1">
                  <a v-if="info.content" class="info-r jzt-shs-medium" :href="`#${info.id}`">{{ info.stastsName }} </a>
                  <span v-else class="jzt-shs-medium"> {{ info.stastsName }}</span>
                  <span v-if="a < item.children.length - 1 && info.children == null" class="jzt-shs-medium">,</span>
                </template>

                <!-- 三级 -->
                <template v-if="info?.isViewProperty === 1">
                  <div class="jzt-shs-medium" v-if="info.children != null">
                    &nbsp; (&nbsp;
                    <a class="info-ye jzt-shs-medium" :href="`#${infos.id}`" v-for="(infos, b) in info.children" :key="b">
                      {{ infos.stastsName }} <span v-if="b < info.children.length - 1">、</span> </a
                    >&nbsp; )<span v-if="a < item.children.length - 1">，</span>
                  </div>
                </template>
                <span v-else>-</span>
              </div>
            </div>
          </div>
          <div class="info-list" v-if="postData.external === 2 || postData.external === 3">
            <div class="info-box">
              <span class="info-f jzt-shs-medium">{{
                postData.external === 2 ? "商家产地：" : postData.external === 3 ? "工厂产地：" : ""
              }}</span>
              <div class="info-r">
                {{ getAddress(postData.external) }}
              </div>
            </div>
          </div>
        </div>
      </div>
      <el-divider class="mt40 mb40" />
    </template>
    <div class="shuxing shop-detail-remark">
      <div class="title mb10">商品详情信息</div>
      <template v-if="postData.content && postData.content !== '<p><br></p>'">
        <div v-html="postData.content"></div>
      </template>
      <el-empty v-else description="这里暂时什么都没有"></el-empty>

      <template v-if="editorsData.length > 0">
        <div style="height: 10px; margin-left: -20px; margin-right: -20px">
          <el-divider class="mt40 mb40" />
        </div>
        <div class="title mb10 mt40">属性说明</div>
        <div class="editor-info" :id="item.id" v-for="(item, i) in editorsData" :key="item.id">
          <div v-html="item.content"></div>
          <!-- <Editor style="height: 100px; overflow-y: auto" :defaultConfig="editorConfig" v-model.trim="item.content"
          :mode="editorModel" @onCreated="editorCreated" /> -->
        </div>
      </template>
    </div>
  </div>
  <el-backtop :bottom="100">
    <div class="upLogo">
      <img src="../imgs/upLogo.png" alt="" />
    </div>
  </el-backtop>
</template>
<script setup>
import { onMounted, ref, reactive } from "vue";
import { getStastsList } from "@/apis/shopHome/detail";
import { useRoute } from "vue-router";
import { Editor } from "@wangeditor/editor-for-vue";
import { shopHomeDetail } from "@/apis/shopCategories/index";
import { generateId } from "@/utils";

const props = defineProps({
  videoUrl: {
    type: String,
    default: ""
  },
  postData: {
    type: Object,
    default: ""
  }
});
const route = useRoute();
const productId = route.query.id;
const attributes = reactive({
  attribute: []
});
const editorInstance = ref(null);
const editorConfig = ref({
  placeholder: "请输入内容...",
  readOnly: true
});
const editorModel = ref("default");

const getAddress = external => {
  let arr = [];
  if (external === 2) {
    //商家产地
    arr = props.postData.buinessMerchantAddress.map(item => {
      return item.buinessAddress.replace(/,/g, "/");
    });
  } else if (external === 3) {
    // 工厂产地
    arr = props.postData.buinessAddress.map(item => {
      return item.buinessAddress.replace(/,/g, "/");
    });
  }
  return arr.join("，");
};

const editorCreated = editor => {
  editorInstance.value = Object.seal(editor);
  editorInstance.value.disable();
};
const editorsData = ref([]);
const getNodesWithContent = data => {
  if (!Array.isArray(data)) return [];
  data.forEach(item => {
    item.children?.length && getNodesWithContent(item.children);
    if (item.content) {
      item.id = generateId();
      editorsData.value.push({
        id: item.id,
        content: item.content
      });
    }
  });
  return data;
};

const getStatic = () => {
  let postData = new FormData();
  postData.append("productId", productId);
  getStastsList(postData)
    .then(res => {
      const data = getNodesWithContent(res.data);
      editorsData.value = editorsData.value.filter(item => item.content).reverse();
      attributes.attribute = data || [];
    })
    .catch(() => {});
};

onMounted(() => {
  getStatic();
});
</script>
<style lang="scss">
.shop-detail-remark {
  img {
    width: 100% !important;
    height: auto !important;
  }
}
img {
  // max-width: 100%;
}

table {
  border-collapse: collapse;
  margin-top: 2px;
  th,
  td {
    border: 1px solid #ccc;
    padding: 5px;
  }
}
div[data-w-e-type="video"] {
  display: flex;
  justify-content: center;
  video {
    width: 100%;
  }
}
</style>
<style lang="scss" scoped>
.shuxing {
  // min-height: 100px;
  background: #fff;
  // margin-bottom: 20px;
  // padding: 0px 20px 20px 20px;

  .title {
    font-weight: 600;
    font-size: 18px;
    color: #1a1a1a;
    line-height: 24px;
    text-align: left;
    font-style: normal;
    text-transform: none;
    // margin-top: 20px;
  }

  .shop-info {
    & > div {
      display: flex;
      flex-wrap: wrap;
      margin-top: 20px;
    }

    // min-height: 100px;

    .info-list {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(25%, 1fr));
      gap: 20px;

      .info-box {
        display: flex;
        a {
          text-decoration: none;
        }
        .info-f {
          margin-right: 40px;
          font-weight: 500;
          font-size: 14px;
          color: #86909c;
          line-height: 20px;
          padding: 0 10px;
          min-width: 100px;
          text-align: right;
        }

        .info-r {
          font-weight: 700;
          font-size: 14px;
          color: #3d3d3d;
          line-height: 20px;

          .info-ye {
            color: #e69e33;
            cursor: pointer;
          }
        }
      }
    }
  }

  .editor-info {
    // min-height: 100px;
  }
}

.upLogo {
  height: 50%;
  width: 50%;
  background-color: var(--el-bg-color-overlay);
  box-shadow: var(--el-box-shadow-lighter);
  text-align: center;
  line-height: 40px;
  color: #1989fa;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
